<?php

Solar::loadClass('Domain51_Util_Service_Connection');

class Domain51_Service_Delicious_Tags  
{
    private $_connection = null;
    
    public function __construct(Domain51_Util_Service_Connection $connection) {
        $this->_connection = $connection;
    }
    
    public function __call($method, $arguments) {
        switch ($method) {
            case 'bundles' :
                $adapter = ucwords($method);
                break;
            
            default:
                throw $this->_exception('Unknown method request');
        }
        
        $adapterClass = 'Domain51_Service_Delicious_Tags_' . $adapter;
        Solar::loadClass($adapterClass);
        return new $adapterClass($this->_connection); 
    }
    
    public function get() {
        $uri = 'tags/get';
        $responseXml = $this->_connection->doRequest($uri);
        
        $array = array();
        foreach ($responseXml->tag as $tag) {
            $tagName = (string)$tag['tag'];
            $tagCount = (int)$tag['count'];
            $array[$tagName] = array(
                'tag' => $tagName,
                'count' => $tagCount
            );
        }
        
        return $array;
    }
    
    public function rename($old, $new) {
        $encodedOld = urlencode($old);
        $encodedNew = urlencode($new);
        $uri = "tags/rename?old={$encodedOld}&new={$encodedNew}";
        
        $responseXml = $this->_connection->doRequest($uri);
        return ((string)$responseXml == 'done');
    }
}

?>